package suites.page {
	import suites.AbstractConfig;
	import suites.stub.AsyncRequestPageStub;
	import suites.stub.InjectorStub;

	import org.flexunit.asserts.fail;
	import org.flexunit.async.Async;
	import org.splink.deepsplink.boot.IDeepsplinkFacade;
	import org.splink.deepsplink.boot.TestBootstrapper;
	import org.splink.deepsplink.config.ConfigDataReader;
	import org.splink.deepsplink.navigation.command.INavigationCommand;
	import org.splink.deepsplink.page.IPage;

	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * @author Max Kugland
	 */
	public class AsyncGotoPageBase extends AbstractConfig {
		private static const PAGE_CLASS_CP : String = "suites.stub.AsyncRequestPageStub";
		private static const PAGE_CLASS : Class = AsyncRequestPageStub;
		protected var _history : Array;
		private var _pages : Array;
		private var _handleEventFnc : Function;

		final protected function handleAsync(timeout : int) : Function {
			return _handleEventFnc = Async.asyncHandler(this, function():void {
			}, timeout, null, handleTimeout);
		}

		private function handleTimeout( passThroughData : Object ) : void {
			fail("Timeout reached before event");			
		}

		final protected function complete() : void {
			_handleEventFnc(new Event(Event.COMPLETE));
		}

		final protected function asyncGotoPage(cmd : INavigationCommand, id : String, params : Array = null) : void {
			var t : Timer = new Timer(1, 1);
			t.addEventListener(TimerEvent.TIMER_COMPLETE, function(e : TimerEvent):void {
				t.removeEventListener(TimerEvent.TIMER_COMPLETE, arguments["callee"]); 				
				clearHistory();
				cmd.gotoPage(id, params);
			});
			t.start();
		}

		final protected function createFacade() : IDeepsplinkFacade {
			_history = new Array();
			_pages = new Array();
			var facade : IDeepsplinkFacade;
			var fnc : Function = function(f : IDeepsplinkFacade) : void {
				facade = f;
				
				facade.addInjector(new InjectorStub(function(page : IPage) : void {
					if(page is AsyncRequestPageStub) {
						AsyncRequestPageStub(page).history = _history;
						_pages.push(page);
					}
				}));
			};
			
			new TestBootstrapper(new ConfigDataReader(<config eagerPages="true">
							<page id={ROOT} clazz={PAGE_CLASS_CP} title={PAGE_TITLE + ROOT} >
								<page id={P_404} clazz={PAGE_CLASS_CP} title={PAGE_TITLE + P_404} />
								<page id="1" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "1"} />
								<page id="2" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "2"}  >
									<page id="3" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "3"} />
									<page id="4" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "4"} />
								</page>
								<page id="5" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "5"}  >
									<page id="6" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "6"} >
										<page id="7" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "7"} />
										<page id="8" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "8"} >
											<page id="9" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "9"} />
											<page id="10" clazz={PAGE_CLASS_CP} title={PAGE_TITLE + "10"} />
										</page>
									</page>
								</page>
							</page>
							<home id={HOME} />
							<notfound redirect={P_404} />
						</config>).read(), createBootStrategy(fnc, pageCount));
			return facade;
		}

		final protected function clearHistory() : void {
			_history.splice(0, _history.length);			
		}

		final protected function getPage(id : String) : AsyncRequestPageStub {
			return _pages.filter(function(p : IPage, index : int, array : Array) : Boolean {
				if(p.id == id) {
					return true;
				}
				return false;
			})[0];
		}
	}
}
